package SnailCatMallWebAppliction;

import JAVAFXManyWindow.impl.GetPostDat;
import JAVAFXManyWindow.pojo.MainMe;
import SnailCatMallWebAppliction.Utils.DataConnection;
import SnailCatMallWebAppliction.domain.Admin;
import SnailCatMallWebAppliction.domain.Classify;
import SnailCatMallWebAppliction.domain.Product;
import SnailCatMallWebAppliction.mapper.AdminMapper;
import SnailCatMallWebAppliction.mapper.ClassifyMapper;
import SnailCatMallWebAppliction.mapper.ProductMapper;
import SnailCatMallWebAppliction.pojo.ValueAndName;
import com.alibaba.fastjson.JSONObject;
import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.ibatis.session.SqlSession;

import java.io.OutputStream;
import java.util.*;

/**
 * Hello world!
 *
 */
public class App extends Application
{
    public static DataConnection dataConnection = new DataConnection();
    public static int items =0;
    public static int ispro=0;
    public static String md5s;
    static {
        List<String> html = new ArrayList<>();
        html.add("index.html");
        html.add("Logins.html");
        html.add("adminroot.html");
        html.add("ProductList.html");
        html.add("ProductInfo.html");
        List<String> javascript = new ArrayList<>();
        javascript.add("webapp/js/jquery-3.4.1.min.js");
        javascript.add("webapp/bootstrap/js/bootstrap.js");
        javascript.add("webapp/Swiper-4.1.0/swiper-4.1.0.min.js");
        javascript.add("webapp/layui/layui.all.js");
        javascript.add("webapp/layui/lay/modules/element.js");
        javascript.add("webapp/layui/layui.js");
        javascript.add("webapp/layui/lay/modules/layer.js");
        javascript.add("webapp/js/echarts-gl.js");
        javascript.add("webapp/layui/lay/modules/table.js");
        javascript.add("webapp/layui/lay/modules/laytpl.js");
        javascript.add("webapp/layui/lay/modules/laypage.js");
        javascript.add("webapp/layui/lay/modules/form.js");
        javascript.add("webapp/layui/lay/modules/util.js");
        javascript.add("webapp/layui/lay/modules/laydate.js");
        javascript.add("webapp/layui/lay/modules/carousel.js");
        javascript.add("webapp/js/tools.js");
        List<String> css = new ArrayList<>();
        css.add("webapp/bootstrap/css/bootstrap.css");
        css.add("webapp/Swiper-4.1.0/swiper-4.1.0.min.css");
        css.add("webapp/css/index.css");
        css.add("webapp/layui/css/layui.css");
        css.add("webapp/css/adminroot.css");
        css.add("webapp/layui/css/modules/code.css");
        css.add("webapp/layui/css/modules/layer/default/layer.css");
        css.add("webapp/layui/css/modules/laydate/default/laydate.css");
        css.add("webapp/css/ProductList.css");
        css.add("webapp/font-awesome-4.7.0/css/font-awesome.css");
        css.add("webapp/css/style.css");
        ispro = MainMe.jxBrowserServerTcp.initcssjs(html,javascript,css);
    }
    public static void main( String[] args )
    {
        try {
            if(MainMe.jxBrowserServerTcp.DatTime(1640966400000L," 对不起你使用的产品已经过期请联系作者续费！否则无法使用！【价格：一年：1000 不维护 | 一年：1500 维护半年】续费邮箱：1012518027@qq.com 订阅  ").equals("go")==true){
                if(ispro>0){
                    SqlSession sqlSession = dataConnection.getSqlsession();
                    AdminMapper adminMapper =sqlSession.getMapper(AdminMapper.class);
                    adminMapper.updateAdminSession("null");
                    sqlSession.commit();
                    sqlSession.close();
                    MainMe.jxBrowserServerTcp.GetJXBrowserServerTcp(MainMe.jxBrowserServerTcp.MacAddressIP4(),1024,  "#EEEEEE", new GetPostDat() {
                        @Override
                        public void dat(List<String> list, String address, OutputStream outputStream) {
                            try {
                                SqlSession sqlSession = dataConnection.getSqlsession();
                                ClassifyMapper classifyMapper = sqlSession.getMapper(ClassifyMapper.class);
                                ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
                                AdminMapper adminMapper =sqlSession.getMapper(AdminMapper.class);
                                Admin admin = adminMapper.selectByPrimaryKey(1);
                                String stage = list.get(0).split("=")[1];
                                if(stage.equals("Logins")==true){
                                    String username = list.get(1).split("=")[1];
                                    String password = list.get(2).split("=")[1];
                                    if(admin.getAdminUser().toString().equals(username)==false || admin.getAdminPassword().equals(password)==false){
                                        MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"密码错误");
                                    }else{
                                        List<String> maclist = MainMe.jxBrowserServerTcp.MacAddress();
                                        for (String s:maclist) {
                                            if(s.equals("00-00-00-00-00-00")==false){
                                                adminMapper.updateAdminSession(s);
                                                sqlSession.commit();
                                                sqlSession.close();
                                                MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,MainMe.jxBrowserServerTcp.setscanUHttp(1024)+"adminroot.html");
                                                return;
                                            }
                                        }

                                    }
                                }
                                if(stage.equals("RAM")==true){
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,String.valueOf(MainMe.jxBrowserServerTcp.Prm(App.class.getPackage().getName())));
                                }
                                if(stage.equals("Itres")==true){
                                    List<ValueAndName> valueAndNameList = new ArrayList<>();
                                    List<Classify> classifyList =  classifyMapper.selectAll();
                                    List<Product> productList = productMapper.selectByAll();
                                    for(int i =0;i<classifyList.size();i++){
                                        ValueAndName valueAndName = new ValueAndName();
                                        for(int j =0;j<productList.size();j++){
                                            if(productList.get(j).getProductMd5().equals(classifyList.get(i).getClassifyMd5())==true){
                                                valueAndName.setName(classifyList.get(i).getClassifyName());
                                                valueAndName.setValue(String.valueOf(items++));
                                            }
                                        }
                                        valueAndNameList.add(valueAndName);
                                        items=1;
                                    }
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,valueAndNameList.toString());
                                }
                                if(stage.equals("classIFY")==true){
                                    int id = Integer.valueOf(list.get(1).split("=")[1]);
                                    int offset= (id-1)*10;
                                    int foot = (id)*10;
                                    List<Classify> classifyList =  classifyMapper.selectpaging(offset,foot);
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"{\"code\":0,\"msg\":\"\",\"data\":"+ JSONObject.toJSONString(classifyList)+"}");
                                }
                                if(stage.equals("PRODUCT")==true){
                                    if(list.get(1).split("=")[0].equals("index")==true){
                                        int id = Integer.valueOf(list.get(1).split("=")[1]);
                                        int offset= (id-1)*10;
                                        int foot = (id)*10;
                                        if(list.get(2).split("=")[0].equals("PRODUCT_MD5")==true){
                                            List<Product> classifyList =  productMapper.selectpagingIF(list.get(2).split("=")[1],offset,foot);
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"{\"code\":0,\"msg\":\"\",\"data\":"+JSONObject.toJSONString(classifyList)+"}");
                                        }else{
                                            List<Product> classifyList =  productMapper.selectpaging(offset,foot);
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"{\"code\":0,\"msg\":\"\",\"data\":"+JSONObject.toJSONString(classifyList)+"}");
                                        }
                                    }
                                    if(list.get(1).split("=")[0].equals("classifyMd5all")==true){
                                        List<Classify> classifyList =  classifyMapper.selectAll();
                                        MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,JSONObject.toJSONString(classifyList));
                                    }
                                }
                                if(stage.equals("SessionID")==true){
                                    List<String> maclist = MainMe.jxBrowserServerTcp.MacAddress();
                                    for (String s:maclist) {
                                        if(s.equals("00-00-00-00-00-00")==false){
                                            String session = adminMapper.selectFirstAdminSession();
                                            if(session.equals("null")==false || s.equals(session)==true){
                                                MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,session);
                                                return;
                                            }
                                        }
                                    }
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"SessionError");
                                }
                                if(stage.equals("PAGING")==true){
                                    double len =0d;
                                    if(list.get(1).split("=")[1].equals("CLASSIFY")==true){
                                        len = classifyMapper.selectAll().size();
                                    }else if(list.get(1).split("=")[1].equals("PRODUCT")==true){
                                        len = productMapper.selectByAll().size();
                                    }
                                    String ints = String.valueOf((double)len/10d);
                                    if(ints.length()>0){
                                        if(ints.split("\\.")[1].equals("0")==true){
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,ints.split("\\.")[0]);
                                        }else{
                                            int as = Integer.valueOf(ints.split("\\.")[0])+1;
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,String.valueOf(as));
                                        }
                                    }
                                }
                                if(stage.equals("ONCLOSE")==true){
                                    adminMapper.updateAdminSession("null");
                                    sqlSession.commit();
                                    sqlSession.close();
                                }
                                if(stage.equals("porsMD5")==true){
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,UUID.randomUUID().toString());
                                }
                                if(stage.equals("addprodurt")==true){
                                    List<Classify> classifyList = classifyMapper.selectAll();
                                    for (Classify c :classifyList) {
                                        if(c.getClassifyMd5().equals(list.get(1).split("=")[1])==true || c.getClassifyName().equals(list.get(2).split("=")[1])==true){
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"类已经存在");
                                            return;
                                        }
                                    }
                                    Classify classify = new Classify();
                                    classify.setClassifyMd5(list.get(1).split("=")[1]);
                                    classify.setClassifyName(list.get(2).split("=")[1]);
                                    classify.setClassifyIcons(MainMe.ImgDat.get(0).getPATH());
                                    classifyMapper.insert(classify);
                                    sqlSession.commit();
                                    sqlSession.close();
                                    MainMe.ImgDat.clear();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"分类添加成功");
                                }
                                if(stage.equals("delectclass")==true){
                                    productMapper.deleteByProductMd5(list.get(1).split("=")[1]);
                                    classifyMapper.deleteByClassifyMd5(list.get(1).split("=")[1]);
                                    sqlSession.commit();
                                    sqlSession.close();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"删除成功");
                                }
                                if(stage.equals("updatectclass")==true){
                                    if(list.get(3).split("=")[1].equals("true")==true){
                                        if(MainMe.ImgDat.size()<1){
                                            classifyMapper.updateClassifyNameAndClassifyMd5(list.get(2).split("=")[1],list.get(1).split("=")[1]);
                                        }else{
                                            classifyMapper.updateClassifyNameAndClassifyMd5classifyIcons(list.get(2).split("=")[1],MainMe.ImgDat.get(0).getPATH(),list.get(1).split("=")[1]);
                                        }
                                    }else{
                                        classifyMapper.updateClassifyNameAndClassifyMd5(list.get(2).split("=")[1],list.get(1).split("=")[1]);
                                    }

                                    sqlSession.commit();
                                    sqlSession.close();
                                    MainMe.ImgDat.clear();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"修改成功");

                                }
                                if(stage.equals("ONPRODUCT")==true){
                                    List<Product> index = productMapper.selectByProductNumber(list.get(2).split("=")[1]);
                                    if(index.size()>0){
                                        MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"添加失败");
                                    }else{
                                        if(MainMe.ImgDat.size()>0){
                                            Product product = new Product();
                                            product.setProductMd5(list.get(1).split("=")[1]);
                                            product.setProductNumber(list.get(2).split("=")[1]);
                                            product.setProductName(list.get(3).split("=")[1]);
                                            product.setProductProperty(list.get(4).split("=")[1]);
                                            product.setProductImages1(MainMe.ImgDat.get(0).getPATH());
                                            product.setProductImages2(MainMe.ImgDat.get(1).getPATH());
                                            product.setProductImages3(MainMe.ImgDat.get(2).getPATH());
                                            product.setProductImages4(MainMe.ImgDat.get(3).getPATH());
                                            product.setProductImages5(MainMe.ImgDat.get(4).getPATH());
                                            product.setProductDate(new Date().toString());
                                            productMapper.insert(product);
                                            sqlSession.commit();
                                            sqlSession.close();
                                            MainMe.ImgDat.clear();
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"添加成功");
                                        }else{
                                            MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"添加失败");
                                        }

                                    }
                                }
                                if(stage.equals("deleteprot")==true){
                                    if(productMapper.selectByProductNumber(list.get(1).split("=")[1]).size()>0){
                                        productMapper.deleteByProductNumber(list.get(1).split("=")[1]);
                                        sqlSession.commit();
                                        sqlSession.close();
                                        MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"删除成功");
                                    }else {
                                        MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"删除失败");
                                    }
                                }
                                if(stage.equals("UPDATEONPRODUCT")==true){
                                    Product product = new Product();
                                    product.setProductId(Integer.valueOf(list.get(6).split("=")[1]));
                                    product.setProductMd5(list.get(2).split("=")[1]);
                                    product.setProductNumber(list.get(3).split("=")[1]);
                                    product.setProductName(list.get(4).split("=")[1]);
                                    product.setProductProperty(list.get(5).split("=")[1]);
                                    if(MainMe.ImgDat.size()>0){
                                        product.setProductImages1(MainMe.ImgDat.get(0).getPATH());
                                        product.setProductImages2(MainMe.ImgDat.get(1).getPATH());
                                        product.setProductImages3(MainMe.ImgDat.get(2).getPATH());
                                        product.setProductImages4(MainMe.ImgDat.get(3).getPATH());
                                        product.setProductImages5(MainMe.ImgDat.get(4).getPATH());
                                    }else{
                                        Product products =productMapper.selectByPrimaryKey(Integer.valueOf(list.get(6).split("=")[1]));
                                        product.setProductImages1(products.getProductImages1());
                                        product.setProductImages2(products.getProductImages2());
                                        product.setProductImages3(products.getProductImages3());
                                        product.setProductImages4(products.getProductImages4());
                                        product.setProductImages5(products.getProductImages5());
                                    }
                                    product.setProductDate(new Date().toString());
                                    productMapper.updateByPrimaryKey(product);
                                    sqlSession.commit();
                                    sqlSession.close();
                                    MainMe.ImgDat.clear();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"修改成功");
                                }
                                if(stage.equals("allclass")==true){
                                    List<Classify> classifyList =  classifyMapper.selectAll();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,JSONObject.toJSONString(classifyList));
                                }
                                if(stage.equals("imtProduct")==true){
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,MainMe.jxBrowserServerTcp.setscanUHttp(1024)+"ProductList.html?md5s="+list.get(1).split("=")[1]);
                                }
                                if(stage.equals("allproducts")==true){
                                    List<Product> productList = productMapper.selectAllByProductMd5AndProductMd5(list.get(1).split("=")[1]);
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,JSONObject.toJSONString(productList));
                                }
                                if(stage.equals("ICONS")==true){
                                    Classify classify = classifyMapper.selectFirstByClassifyMd5(list.get(1).split("=")[1]);
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,classify.getClassifyIcons());
                                }
                                if(stage.equals("allproductsInFO")==true){
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,MainMe.jxBrowserServerTcp.setscanUHttp(1024)+"ProductInfo.html?PRODUCT_NUMBER="+list.get(1).split("=")[1]+"&MD5="+list.get(2).split("=")[1]);
                                }
                                if(stage.equals("LisgPRODUCT_NUMBER")==true){
                                    Product product = productMapper.selectFirstByProductNumber(list.get(1).split("=")[1]);
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,JSONObject.toJSONString(product));
                                }
                                if(stage.equals("claryAll")==true){
                                    MainMe.ImgDat.clear();
                                    MainMe.jxBrowserServerTcp.AjaxGetResponse(outputStream,"OK");
                                }
                            }catch (Exception e){e.printStackTrace();}
                        }

                        @Override
                        public void addressUrl(String s) {

                        }
                    });
                }
            }

        }catch (Exception e){e.printStackTrace();}
        MainMe.jxBrowserServerTcp.AddressURL(MainMe.jxBrowserServerTcp.setscanUHttp(1024)+"index.html");
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

    }
}
